package ui;

import game.AudioEngine;

import java.util.ArrayList;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.gui.AbstractComponent;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.state.transition.FadeInTransition;
import org.newdawn.slick.state.transition.FadeOutTransition;
import org.newdawn.slick.util.Log;

public class GameOptionState extends BaseMenuState {
	private boolean isFullscreen;
	private boolean isSoundOn = true;
	private int resolutionIndex;
	private int resolution;
	private int[] resolutionArray = new int[]{800, 1024, 1920};

	public GameOptionState() {
		super();
	}
	
	@Override
	public void init(GameContainer gc, StateBasedGame game) throws SlickException {
		this.isFullscreen = gc.isFullscreen();
		this.isSoundOn = gc.isSoundOn();
		this.resolution = gc.getWidth();
		resolutionIndex = 0;
		
		// draw menu buttons - new game, options, exit
		addButton(gc, "Resolution", -1);
		addButton(gc, "Fullscreen", -1);
		addButton(gc, "Sound", -1);
		addButton(gc, "Confirm", GameStateSwitcher.MENU_STATE);
		addButton(gc, "Cancel", GameStateSwitcher.MENU_STATE);
	}

	@Override
	public void render(GameContainer gc, StateBasedGame state, Graphics g) throws SlickException {
		g.drawImage(new Image("resources/menu/title.png"),
				gc.getWidth() / 2 - titleWidth / 2, componentSpacing);
		for (ButtonComponent button : menuItems){
			button.render(gc, g);
			if (button.getText().equals("Resolution")) {
				g.drawString(resolution + "x" + getResolutionHeight(resolution), button.getX() + 300, button.getY() + button.getHeight() / 2);
			}
			if (button.getText().equals("Fullscreen")) {
				g.drawString(isFullscreen ? "Yes" : "No", button.getX() + 300, button.getY() + button.getHeight() / 2);
			}
			if (button.getText().equals("Sound")) {
				g.drawString(isSoundOn ? "On" : "Off", button.getX() + 300, button.getY() + button.getHeight() / 2);
			}
		}
	}

	@Override
	public void update(GameContainer gc, StateBasedGame state, int delta) throws SlickException {
		// check if a button is clicked
		for (ButtonComponent button : menuItems) {
			button.update(gc);
			if (button.isClicked()){
				int stateID = button.getStateID();
				String buttonText = button.getText();
				try {
					if (stateID != -1){
						if (buttonText.equals("Confirm")){
							// lock in new options, (nasty hack, sorry)
							if (isFullscreen) {
								((GameWindowContainer)gc).setDisplayMode(gc.getScreenWidth(), gc.getScreenHeight(), isFullscreen);
							}
							else {
								((GameWindowContainer)gc).setDisplayMode(resolution, getResolutionHeight(resolution), isFullscreen);
							}
							gc.setFullscreen(isFullscreen);
							gc.setSoundOn(isSoundOn);
							AudioEngine.setMaxVolume(isSoundOn ? 1f : 0f);
							// TODO AudioEngine.setMaxVolume();
						}
						else {
							// else reset the values
							this.isFullscreen = gc.isFullscreen();
							this.isSoundOn = gc.isSoundOn();
							this.resolution = gc.getWidth();
						}
						state.enterState(stateID, new FadeOutTransition(), new FadeInTransition());
					}
					else {
						if (buttonText.equals("Resolution")){
							toggleResolution();
						}
						if (buttonText.equals("Fullscreen")){
							toggleFullscreen();
						}
						if (buttonText.equals("Sound")){
							toggleIsSoundOn();
						}
					}
				} catch (SlickException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	private void toggleResolution() {
		resolutionIndex = resolutionIndex + 1 >= resolutionArray.length ? 0 : resolutionIndex + 1;
		resolution = resolutionArray[resolutionIndex];
	}

	private void toggleFullscreen() {
		isFullscreen = !isFullscreen;
	}
	private void toggleIsSoundOn() {
		isSoundOn = !isSoundOn;
	}

	@Override
	public int getID() {
		return GameStateSwitcher.OPTION_STATE;
	}
	
	private void addButton(GameContainer gc, String buttonText, int stateID) {
		ButtonComponent newButton = new ButtonComponent(gc, buttonText, menuItems.size(), stateID);
		menuItems.add(newButton);
	}
	
	private int getResolutionHeight(int resolutionWidth){
		if (resolutionWidth == 800)
			return 800;
		if (resolutionWidth == 1024)
			return 720;
		if (resolutionWidth == 1920)
			return 1080;
		return 600;
	}

	public static final int RESOLUTION_800 = 800;
	public static final int RESOLUTION_1024 = 1024;
	public static final int RESOLUTION_1920 = 1920;

}
